package me.zhengjie.controller;
import lombok.RequiredArgsConstructor;
import me.zhengjie.nettydemo.nettty.LiftCenter;
import me.zhengjie.pojo.dto.LiftRequestDto;
import me.zhengjie.pojo.entity.Lift;
import me.zhengjie.service.LiftTaskService;
import me.zhengjie.sqlservilce.LiftSqlService;
import me.zhengjie.state.TaskType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/lift")
@RequiredArgsConstructor
public class LiftController {

    private final LiftCenter liftCenter;

    @Autowired
    private final LiftTaskService liftTaskService;

    private final LiftSqlService liftSqlService;

//    public LiftController(LiftCenter liftCenter, LiftTaskService liftTaskService) {
//        this.liftCenter = liftCenter;
//        this.liftTaskService = liftTaskService;
//    }

    //获取token
    @PostMapping("/developerLogin")
    public  String  developerLogin(String robotId) throws Exception {
        return liftTaskService.developerLogin(robotId);
    }

    //预约单云电梯
    @PostMapping("/callElevator")
    private  String callElevator(LiftRequestDto liftRequestDto) throws Exception {
        return liftTaskService.callElevator(liftRequestDto);
    }


    //设置回调地址
    @PostMapping("/setCallBack")
    private  String setCallBack(LiftRequestDto liftRequestDto) throws Exception {
        return liftTaskService.setCallBack(liftRequestDto);
    }

    //发送开门指令
    @PostMapping("/sendOpenDoor")
    private  String sendOpenDoor(LiftRequestDto liftRequestDto) throws Exception {
        return liftTaskService.sendOpenDoor(liftRequestDto);
    }

    /**
     * 接收 /encryptScript 路径的 POST 请求，
     * Content-Type 必须是 application/x-www-form-urlencoded
     */
    @PostMapping(value = "/callback/url",
            consumes = "application/x-www-form-urlencoded")
    @ResponseBody
    public void receive(@RequestParam("encryptScript") String encryptScript) throws Exception {
        // 这里只是把收到的密文打印出来，实际业务可自行处理
        System.out.println("收到 encryptScript：");
        System.out.println(encryptScript);
        liftTaskService.callbackUrl(encryptScript);
//        return "OK";
    }

    //新电梯模式切换

    @GetMapping("move/{status}")
    public void liftMove(@PathVariable String status) throws Exception {
        if (Objects.equals(status, "0")){
            boolean b = liftCenter.handMovement(TaskType.NEW_FLOOR.getMessage());
        }else if (Objects.equals(status, "1")){
            //自动模式
            boolean b = liftCenter.liftMove(TaskType.NEW_FLOOR.getMessage());
        }
    }

    @GetMapping("closeDoor")
    public void closeDoor(){
        boolean b = liftCenter.closeDoor(TaskType.NEW_FLOOR.getMessage());
    }

    @GetMapping("test01/{eqptCode}")
    public boolean test01(@PathVariable String eqptCode){

        if (TaskType.NEW_FLOOR.getMessage().equals(eqptCode)){
            return true;
        }
        return false;
    }

}
